package com.sanrenzu.everybodytv.ui.recommend.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.bean.sms.CateBean;
import com.sanrenzu.everybodytv.ui.recommenditent.RecommendIntentActivity;
import com.sanrenzu.everybodytv.widget.GlideRoundTransform;

import java.util.List;

/**
 * Created by asus on 2016/9/6.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter {

    private List<CateBean> list;
    private Context mContext;
    private LayoutInflater mInflater;
    private RecyclerView recyclerView;

    public RecyclerViewAdapter(Context mContext, List<CateBean> list,RecyclerView recyclerView) {
        this.mContext = mContext;
        this.list = list;
        this.recyclerView = recyclerView;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_item_commend, parent, false);
        //单击事件跳转到一个GirdView下
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"点击了广告",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, RecommendIntentActivity.class);
                Bundle bundle = new Bundle();
                int position = recyclerView.getChildAdapterPosition(v);
                bundle.putString("classification", list.get(position).getExt().getClassification());
                bundle.putString("subtitle",list.get(position).getSubtitle());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder mHolder = (MyViewHolder) holder;
        mHolder.textViewItemRecycleTitle.setText(list.get(position).getTitle());
        RequestManager manager = Glide.with(mContext);
        manager.load(list.get(position).getThumb())
                .transform(new GlideRoundTransform(mContext,90))
                .into(mHolder.imageViewItemRecycleIcon);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewItemRecycleIcon;
        TextView textViewItemRecycleTitle;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageViewItemRecycleIcon = (ImageView) itemView.findViewById(R.id.imageView_item_Recycle_icon);
            textViewItemRecycleTitle = (TextView) itemView.findViewById(R.id.imageView_item_recycle_title);
        }
    }
}
